using Godot;
using System;
using System.Linq;

public partial class PilotSkillSelection : Node2D
{
    [Signal]
    public delegate void OnSkillItemSelectEventHandler(BaseSkill skill);

    [Signal]
    public delegate void OnUIAnimateEndEventHandler();

    PanelContainer panel;
    Control skillRoot;
    GridContainer skillList;
    UIButton closeButton;

    PackedScene itemPrefab = ResourceLoader.Load<PackedScene>(Path.SKILL_SELECTION_ITEM);

    static LevelInfo UILevelInfo => GameManager.Instance.LevelManager.UILevelInfo;

    public override void _Ready()
    {
        panel = GetNode<PanelContainer>("%Pannel");
        skillRoot = GetNode<Control>("%SkillRoot");
        skillList = GetNode<GridContainer>("%SkillList");
        closeButton = GetNode<UIButton>("%CloseButton");
        closeButton.OnPressed += AnimateHide;
    }

    private void AnimateHide(UIButton button)
    {
        tween?.Stop();
        tween = CreateTween();
        tween.TweenProperty(skillRoot, "position", startPos, 0.2f);
        tween.Finished += OnTweenFinshedHandler;
        panel.Hide();
    }

    private void OnTweenFinshedHandler()
    {
        EmitSignal(SignalName.OnUIAnimateEnd);
    }

    Tween tween;
    Vector2 startPos = new(576, 1296);
    Vector2 endPos = new(576, 648);

    public void AnimateShow(Robot robot)
    {
        panel.Show();
        var sks = robot.Pilot.SkillList;//.FindAll(v => v.Cost[SkillCostType.Sprite].As<int>() <= robot.Entity.TotalSprite);
        var children = skillList.GetChildren();
        var diff = sks.Count - children.Count;
        int i = 0;
        foreach (SkillSelectionItem c in children.Cast<SkillSelectionItem>())
        {
            if (i < sks.Count)
            {
                c.Init(sks[i]);
                i++;
            }
            else
            {
                c.Hide();
            }
        }
        if (diff > 0)
        {
            for (int j = 0; j < diff; j++)
            {
                var item = itemPrefab.Instantiate<SkillSelectionItem>();
                skillList.AddChild(item);
                item.Init(sks[i]);
                item.OnSkillSelect += OnSkillItemSelectHandler;
                i++;
            }
        }

        tween?.Stop();
        tween = CreateTween();
        tween.TweenProperty(skillRoot, "position", endPos, 0.2f);
    }

    private async void OnSkillItemSelectHandler(BaseSkill skill)
    {
        if (skill.TryCast())
        {
            AnimateHide(null);
            await ToSignal(this, SignalName.OnUIAnimateEnd);
            // 无目标类型直接释放
            if (skill.CastType == SkillCastType.NoTarget)
            {
                var e = new SkillEventEntity
                {
                    Skill = skill
                };
                skill.Cast(e);
                await ToSignal(skill, nameof(skill.OnCastEnd));
            }
            else if (skill.CastType == SkillCastType.Target)
            {
                UILevelInfo.WarningMessage("没有可以作用的目标");
                EmitSignal(SignalName.OnSkillItemSelect, skill);
            }
        }
        else
        {
            GD.Print("无法施放");
        }
    }

}
